<?php

namespace Admin\Controller;
use Org\Util\Sync;
use Org\Util\Rest;
use Think\Model;
use Org\BMap; 

/**
 * 测试
 */
class TestController extends AdminController {
    
    public function test(){
        $reuslt = $this -> pull1("Order");
    }
    
    /**
     * 下拉
     */
    public function pull1($className){
        
        $cql = urlencode('select count(*) from '.$className.' where syncStatus<>0');
        $rest = new Rest();
        //获取数据总数
        $result = $rest->query('cql='.$cql);
        if($result->body->count){
            $count = $result->body->count;
            
            $page = ceil($count / 10);    // 计算要分的页数
            
            for ($i=0; $i<$page; $i++){
                $start = $i*10;
                $end = $start + 10;
                //查询数据
                $cql = urlencode('select * from '.$className.' where syncStatus<>0 limit '.$start.','.$end);
                $result = $rest->query('cql='.$cql);
                
                if($result->body->results){
                    $data = array();
                    $results = $result->body->results;
                    
                    foreach ($results as $o){
                        $params=array('objectId'=> $o->objectId,'syncStatus'=>2);
                        $syncStatus = $o->syncStatus;
                        
                        $GeoUtils=new BMap\GeoUtils(); 
                        $areaId=$GeoUtils->isPointInArea($o->location->longitude,$o->location->latitude);
                        
                        $odata['payway'] = $o->payway;
                        $odata['toArea'] = $o->address->area;
                        $odata['uid'] = $o->uid;
                        $odata['finalAmount'] = $o->total;
                        $odata['toCity'] = $o->address->city;
                        $odata['toProvince'] = $o->address->province;
                        $odata['payDetails'] = $o->payDetails;
                        $odata['toName'] = $o->address->name;
                        $odata['toPhone'] = $o->address->phone;
                        $odata['toStreet'] = $o->address->address;
                        $odata['toLon'] = $o->location->longitude;
                        $odata['toLat'] = $o->location->latitude;
                        $odata['areaId'] = $areaId;
                        $odata['freight'] = 0;
                        $odata['status'] = $o->status;
                        $odata['syncStatus'] = 0;
                        $odata['createDate'] = strtotime($o->createdAt);
                        $odata['lastupdate'] = strtotime($o->updatedAt);
                        
                        //未对号入座
                        $odata['amount'] = $o->amount;
                        $odata['cancelTime'] = $o->cancelTime;
                        $odata['payTime'] = $o->payTime;
                        $odata['sendTime'] = $o->sendTime;
                        $odata['takeTime'] = $o->takeTime;
                        $odata['usablePoint'] = $o->usablePoint;
                        $odata['usedPoint'] = $o->usedPoint;
                        $odata['givePoint'] = $o->givePoint;
                        $odata['isSince'] = $o->isSince;
                        $odata['invoice'] = $o->invoice;
                        $odata['remark'] = $o->remark;
                        $odata['logisticsNo'] = $o->logisticsNo;
                        $odata['logistics'] = $o->logistics;
                        
                        $goods = $o->goods;
                        foreach ($goods as $g){
                            $gdata['orderid'] = $g->orderid;
                            $gdata['specid'] = $g->sid;
                            $gdata['cartnum'] = $g->size;
                            
                            $gdata['price'] = $g->price;
                            $gdata['point'] = $g->point;
                            $gdata['totalprice'] = $g->totalprice;
                            
                            $result =M('order_goods')->data($gdata)->add();
                        }
                        
                        if($syncStatus == 1){
                            //新增
                            $odata['orderid'] = $o->orderid;
                            $result =M($className)->data($odata)->add(); 
                        }else{
                            if($syncStatus == 3){
                                $o->status=-1;
                                $params['syncStatus'] = 3;
                            }
                            
                            //修改
                            M($className)->where(array('orderid' => $o->orderid))->save($odata);
                        }
                        array_push($data, $params);
                    }
                    
                    if(count($data) > 0){
                        $results = $rest->batch(array('className' => $className, 'results' => $data));
                }
            }
        }
        }
        return array('code'=>0);
    }

}
